package spring_test;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring_test.dao.PersonDao;
import spring_test.dao.imp.PersonDaoImpl;
import spring_test.entity.Person;

/**
 * TODO
 * 
 * @author cuiran
 * @version TODO
 */
public class MongoTest {

	private static Log log = LogFactory.getLog(MongoTest.class.getName());

	private static PersonDao pr = null;

	/**
	 * 
	 * <b>function:</b>
	 * 
	 * @author cuiran
	 * @createDate 2012-12-12 16:08:02
	 */
	public static void init() {
		log.debug("开始启动");
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context-mongodb.xml");
		pr = (PersonDaoImpl) ctx.getBean("PersonDao");

	}

	/**
	 * 
	 * <b>function:</b>添加
	 * 
	 * @author cuiran
	 * @createDate 2012-12-12 16:11:01
	 */
	public void insert() {

		Person p = new Person("89757", "cuiran", 27);
		pr.insert(p);
		log.debug("添加成功");
	}

	/**
	 * 
	 * <b>function:</b>根据输入的ID查找对象
	 * 
	 * @author cuiran
	 * @createDate 2012-12-12 16:24:10
	 */
	public void findOne(String id) {
		Person p = pr.findOne(id);
		System.out.println(p.toString());
	}

	public void update(Person person) {
		pr.findAndModify(person);
		Person p = pr.findOne(person.getId());
		System.out.println(p.toString());
	}

	/**
	 * 
	 * <b>function:</b>查询所有
	 * 
	 * @author cuiran
	 * @createDate 2012-12-12 16:08:54
	 */
	public void listAll() {

		List<Person> list = pr.findAll();
		log.debug("查询结果如下:");
		for (Person p : list) {
			System.out.println(p.toString());
		}

	}

	/**
	 * 
	 * <b>function:</b>测试方法
	 * 
	 * @author cuiran
	 * @createDate 2012-12-12 16:11:37
	 */
	/*
	 * public void start(){ init();
	 * 
	 * //insert(); //listAll();
	 * 
	 * //findOne(); }
	 */

	/**
	 * <b>function:</b>main函数
	 * 
	 * @author cuiran
	 * @createDate 2012-12-12 11:54:30
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MongoTest.init();
		MongoTest test = new MongoTest();
		Person p = pr.findOne("89757");
		p.setAge(29);
		p.setName("lxx");
		test.update(p);
	}

}